package com.yc.supermarket.web.controller;

import com.yc.supermarket.entity.Commodity;
import com.yc.supermarket.entity.ProductType;
import com.yc.supermarket.entity.Sales;
import com.yc.supermarket.entity.Transaction;
import com.yc.supermarket.mapper.GoodsInfoMapper;
import com.yc.supermarket.service.ProductTypeService;
import com.yc.supermarket.service.SalesService;
import com.yc.supermarket.service.TransactionService;
import com.yc.supermarket.service.impl.ProductTypeServiceImpl;
import com.yc.supermarket.service.impl.SalesServiceImpl;
import com.yc.supermarket.service.impl.TransactionServiceImpl;
import com.yc.supermarket.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/sendGoods")
public class SendGoodsController extends BaseController {

    private TransactionService service = new TransactionServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String op=req.getParameter("op");
        if("show".equals(op)){
            doShow(req,resp);
        }else if("select".equals(op)){
            doSelect(req,resp);
        }else  if("send".equals(op)){
            doSend(req,resp);
        }else   if("detail".equals(op)){
            doDetail(req,resp);
        }else   if("selectByUserId".equals(op)){
            doSelectByUserId(req,resp);
        }else {
            printToJson(resp,"No method.....405");
        }
    }

    protected void doSelectByUserId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer userId =  Integer.parseInt(req.getParameter("userId"));
        Map<String, Object> byUserId = service.findByUserId(userId);
       /* GoodsInfoMapper mapper1;
        SqlSession session = MybatisUtil.getSession();
        mapper1=session.getMapper(GoodsInfoMapper.class);
        List<Commodity> commoditiesByComId = mapper1.getCommoditiesByComId(1);*/
        printToJson(resp,byUserId);
    }

    protected void doShow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String,Object> map = service.findByStatus(0);
        System.out.println(map.get("data"));
        printToJson(resp,map);
    }

    protected void doSelect(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String,Object> map = parseRequest(req);
        Map<String,Object> result = service.findByTrem(map);
        System.out.println(result.get("data"));
        printToJson(resp,result);
    }

    protected void doSend(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("transactionId");
        boolean result = service.modifyStatus(id);
        System.out.println(id);
        printToJson(resp,result);
    }

    protected void doDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Sales sales = parseRequest(req,Sales.class);
        SalesService se = new SalesServiceImpl();
        Map<String,Object> map = se.finds(sales);
        printToJson(resp,map);
    }

}
